package com.quyunshuo.module.home.activity.report

import android.content.Context
import android.content.Intent
import android.util.Log
import androidx.activity.viewModels
import androidx.core.view.isVisible
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.quyunshuo.edwingoal.common.ui.BaseActivity
import com.quyunshuo.module.home.activity.main.MainActivity
import com.quyunshuo.module.home.activity.main.SharedViewModel
import com.quyunshuo.module.home.databinding.HomeActivityReportBinding
import com.quyunshuo.module.home.room.entity.Steps
import com.quyunshuo.module.home.util.getDateParts
import com.quyunshuo.module.home.util.getMonthName
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
import java.util.Locale

@AndroidEntryPoint
class ReportActivity : BaseActivity<HomeActivityReportBinding, ReportViewModel>() {

    override val mViewModel by viewModels<ReportViewModel>()
    override fun createVB(): HomeActivityReportBinding {
        return  HomeActivityReportBinding.inflate(layoutInflater)
    }
    companion object {
        fun newInstance(context: Context) {
            context.startActivity(Intent(context, ReportActivity::class.java))
        }
    }
    private val dateFormatter by lazy {
        SimpleDateFormat("MMMM dd, yyyy", Locale.getDefault())
    }

    override fun initObserve() {
        // 当前步数数据
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                mViewModel.currentStep.collect { steps ->
                    steps?.let { showData(it) }
                }
            }
        }

        // 按钮状态
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                mViewModel.hasPrevious.collect { hasPrev ->
                    mBinding.previous.isVisible = hasPrev
                }
            }
        }

        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                mViewModel.hasNext.collect { hasNext ->
                    mBinding.next.isVisible = hasNext
                }
            }
        }

        // 空数据状态
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                mViewModel.isEmpty.collect { empty ->
                    if (empty) {
                        showEmptyState()
                    } else {
                        hideEmptyState()
                    }
                }
            }
        }
    }

    override fun initRequestData() {
        // 可以在这里触发数据加载
    }

    override fun HomeActivityReportBinding.initListener() {
        mBinding.previous.setOnClickListener {
            mViewModel.previousStep()
        }
        mBinding.next.setOnClickListener {
            mViewModel.nextStep()
        }
    }

    override fun HomeActivityReportBinding.initView() {
        // 初始化UI状态
        mBinding.previous.isVisible = false
        mBinding.next.isVisible = false
    }

    private fun showData(steps: Steps) {
        mBinding.stepCount.text = steps.stepCount.toString()
        mBinding.date.text = dateFormatter.format(steps.date)
    }

    private fun showEmptyState() {
        // 显示空数据UI
    }

    private fun hideEmptyState() {
        // 隐藏空数据UI
    }
}